package cn.jellybmedia.org.service.impl;

import cn.jellybmedia.basic.service.impl.BaseServiceImpl;
import cn.jellybmedia.org.domain.Department;
import cn.jellybmedia.org.mapper.DepartmentMapper;
import cn.jellybmedia.org.service.IDepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @Description: service层实现类
* @Author: Mr.Zhang
* @Version: 1.0
* @Date:  2022/6/21 18:10  
*/

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;



    /**
     * @Title: save
     * @Description: 新增部门
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date: 2022/6/21 18:08
     * @Parameters: [department]
     * @Return void
     */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath="";
        // 判断这个部门是否父亲,如果没有,那么就是一级部门
        if (department.getParent().getId() == null){
            // 如果没有父亲,那么上级部门路径就是就是他自己id
            dirPath = "/"+department.getId();
        }else {
            // 如果有父亲,那么就是子级部门,上级部门路径,就是他父亲的id最后加上他自己的id
            Department departmentParent = departmentMapper.loadById(department.getParent().getId());
            // 因为可能父亲中有些数据是脏数据,存在没有id值.所以需要进行判断
            if (departmentParent != null){
                dirPath=departmentParent.getDirPath()+"/"+department.getId();
            }
        }
        // 把路径设置到部门中去
        department.setDirPath(dirPath);
        // 调用修改方法进行修改
        departmentMapper.update(department);
    }

    /**
     * @Title: update
     * @Description: 修改部门数据
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date: 2022/6/21 18:08
     * @Parameters: [department]
     * @Return void
     */
    @Override
    @Transactional
    public void update(Department department) {
        String dirPath="";
        // 判断这个部门是否父亲,如果没有,那么就是一级部门
        if (department.getParent().getId() == null){
            // 如果没有父亲,那么上级部门路径就是就是他自己id
            dirPath = "/"+department.getId();
        }else {
            // 如果有父亲,那么就是子级部门,上级部门路径,就是他父亲的id最后加上他自己的id
            Department departmentParent = departmentMapper.loadById(department.getParent().getId());
            // 因为可能父亲中有些数据是脏数据,存在没有id值.所以需要进行判断
            if (departmentParent != null){
                dirPath=departmentParent.getDirPath()+"/"+department.getId();
            }
        }
        // 把路径设置到部门中去
        department.setDirPath(dirPath);
        // 调用修改方法进行修改
        departmentMapper.update(department);
    }




    /**
     * @Title: getTree
     * @Description: 获取部门树
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date: 2022/6/26 8:49
     * @Parameters: []
     * @Return java.util.List<cn.jellybmedia.org.domain.Department>
     */
    @Override
    public List<Department> getTree() {
        // 获取全部的部门数据
        List<Department> departments = departmentMapper.loadAll();
        // 创建一个部门集合,用于存放新的部门树数据
        ArrayList<Department> departmentTree = new ArrayList<>();
        // 将获取到的全部部门数据转换成map集合
        HashMap<Long, Department> departmentMap = new HashMap<>();
        for (Department department : departments) {
            departmentMap.put(department.getId(),department);
        }


        // 进入for循环,循环得到的全部部门数据
        for(Department deptTmp:departments){
            // 判断数据是否有父级id,如果没有,就是是第一级,
            if (deptTmp.getParentId() == null){
                departmentTree.add(deptTmp);
            }else {
                // 如果有,就不是第一季,就通过自己id去找父亲的子级id
                Department department = departmentMap.get(deptTmp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }

            }
        }
        return departmentTree;
    }

}
